package com.arch.dev.client.view;


import com.arch.dev.client.vo.AuthorityVO;
import com.arch.dev.client.vo.UserVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UserFormView extends Composite{
	
	private static final String ADMIN_USER = "Admin";
	
	private static final String ADMIN_USER_ROLE = "ROLE_ADMIN";
	
	private static final String NORMAL_USER = "User";
	
	private static final String NORMAL_USER_ROLE = "ROLE_USER";

	private static UserFormViewUiBinder uiBinder = GWT
			.create(UserFormViewUiBinder.class);

	interface UserFormViewUiBinder extends UiBinder<Widget, UserFormView> {
	}

	@UiField(provided = true)
	FlexTable formTable;

	TextBox usernameTextBox;
	
	PasswordTextBox pwdTextBox; 
	
	ListBox authListBox;
	
	CheckBox enabledCheckBox;

	@UiField
	Button cancelButton;

	public Button getCancelButton() {
		return cancelButton;
	}

	public void setCancelButton(Button cancelButton) {
		this.cancelButton = cancelButton;
	}

	@UiField
	Button saveButton;
	
	public UserFormView() {
		init();

		initWidget(uiBinder.createAndBindUi(this));
	}

	private void init() {
		formTable = new FlexTable();

		formTable.setCellSpacing(6);

		usernameTextBox = new TextBox();
		pwdTextBox = new PasswordTextBox();
		
		authListBox = new ListBox();
		
		enabledCheckBox = new CheckBox();

		populateFields();

		formTable.setHTML(1, 0, "Username :");

		formTable.setWidget(1, 1, usernameTextBox);
		
		formTable.setHTML(2, 0, "Password :");

		formTable.setWidget(2, 1, pwdTextBox);
		
		formTable.setHTML(3, 0, "Authority:");
		formTable.setWidget(3, 1, authListBox);
		
		formTable.setHTML(4, 0, "Enabled:");
		formTable.setWidget(4, 1, enabledCheckBox);


	}

	public void saveButtonAddClickHandler(ClickHandler handler){
		saveButton.addClickHandler(handler);
	}
	
	public UserVO populateVo() {
		
		UserVO userVO = new UserVO();
		AuthorityVO authorityVO= new AuthorityVO();
		userVO.setUserName(usernameTextBox.getText().trim());
		userVO.setPassWord(pwdTextBox.getText().trim());
		userVO.setEnabled(enabledCheckBox.getValue());
		authorityVO.setUserName(userVO.getUserName());
		authorityVO.setAuthority(authListBox.getValue(authListBox.getSelectedIndex()));
		userVO.setAuthorityVO(authorityVO);	
		if(this.userVO != null){
			userVO.setCreatedDate(this.userVO.getCreatedDate());
			userVO.setLastUpdated(this.userVO.getLastUpdated());
			authorityVO.setCreatedDate(this.userVO.getAuthorityVO().getCreatedDate());
			authorityVO.setLastUpdated(this.userVO.getAuthorityVO().getLastUpdated());
		}
		
		return userVO;


	}
	
	
	public void populateFields() {	

		authListBox.addItem(NORMAL_USER, NORMAL_USER_ROLE);
		authListBox.addItem(ADMIN_USER, ADMIN_USER_ROLE);

	}
	
	private UserVO userVO;
	
	public void populateFormFields(UserVO userVO) {
		this.userVO = userVO;
		usernameTextBox.setText(userVO.getUserName());
		pwdTextBox.setText(userVO.getPassWord());
		pwdTextBox.setEnabled(false);
		if (userVO.getAuthorityVO().getAuthority()
				.equalsIgnoreCase(NORMAL_USER_ROLE)) {
			authListBox.setSelectedIndex(0);
		} else if (userVO.getAuthorityVO().getAuthority()
				.equalsIgnoreCase(ADMIN_USER_ROLE)) {
			authListBox.setSelectedIndex(1);
		}
	   enabledCheckBox.setValue(userVO.getEnabled());
	}
	
	public void initFormFields(){
		usernameTextBox.setText("");
		pwdTextBox.setText("");
		pwdTextBox.setEnabled(true);
		authListBox.setSelectedIndex(0);		
	    enabledCheckBox.setValue(false);
	}
}
